package com.yiwei.zxp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.allen.library.SuperButton;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.BuyerOrderActivity;
import com.yiwei.zxp.adapter.MyCommentAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.UserSellCommentListBean;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.view.OnLoadeMoreListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-7-31.
 */

public class MyCommentFragment extends BaseFragment {
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    Unbinder unbinder;
    @BindView(R.id.btn_comment)
    SuperButton btnComment;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private MyCommentAdapter adapter;
    private String keyWord;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_comment;
    }

    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getUserCommentLists(page);
        }
    };

    //买家版我的评价
    @Override
    protected void initView() {
        if (getArguments() != null) {
            keyWord = getArguments().getString("keyword", "");
        }
//        （0：全部，1：好评，2：中评，3：差评，4：有图）不传默认为0
        if (keyWord.equals("1")) {
            keyWord = "4";
        } else if (keyWord.equals("2")) {
            keyWord = "1";
        } else if (keyWord.equals("3")) {
            keyWord = "2";
        } else if (keyWord.equals("4")) {
            keyWord = "3";
        }
        adapter = new MyCommentAdapter(getContext());
        list.addOnScrollListener(onLoadeMoreListener);
        list.setAdapter(adapter);
        onLoadeMoreListener.setPage();
        getUserCommentLists(1);
    }


    public void getUserCommentLists(int page) {
        //    "全部", "待付款", "已付款"
        //状态（0：过期，1:有效，2：已使用)
        if (!Network.checkNetwork(getContext())) {
            ToastUtil.show(getContext(), "网络连接失败");
            return;
        }
        if (refresh != null) {
            refresh.setRefreshing(true);
        }
        UserSellCommentListBean bean = new UserSellCommentListBean();
        bean.keyword = keyWord;
        bean.page = page;
        Api.api_service.getUserCommentLists(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.btn_comment)
    public void onViewClicked() {
        Intent intent3 = new Intent(getActivity(), BuyerOrderActivity.class);
        intent3.putExtra("poi", 4);
        startActivity(intent3);
        getActivity().finish();
        getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);

    }
}
